package uselessmouths.mainservices.service;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import uselessmouths.mainservices.entities.Terminal;

public class TerminalsService {

    private static TerminalsService instance;
    private EntityManager entityManager;

    private TerminalsService() {
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("useless-mouths_MainServices_jar_1.0-SNAPSHOTPU");
        entityManager = entityManagerFactory.createEntityManager();
    }

    public static TerminalsService getInstance() {
        if (instance == null) {
            instance = new TerminalsService();
        }
        return instance;
    }

    public List<Terminal> getAllTerminals() {
        entityManager.getTransaction().begin();
        List<Terminal> resultList = entityManager.createNamedQuery("Terminal.findAll").getResultList();
        entityManager.getTransaction().commit();
        return resultList;
    }
    
    public List<Terminal> getTerminalsById(String recId) {
        entityManager.getTransaction().begin();
        List<Terminal> resultList = entityManager.createNamedQuery("Terminal.findByRecid").setParameter("recid", recId).getResultList();
        entityManager.getTransaction().commit();
        return resultList;
    }
    
   public List<Terminal> getTerminalsByTeminalName(String name) {
        entityManager.getTransaction().begin();
        List<Terminal> resultList = entityManager.createNamedQuery("Terminal.findByName").setParameter("name", name).getResultList();
        entityManager.getTransaction().commit();
        return resultList;
    }
    
   public List<Terminal> getTerminalsByUserId(String userId) {
        entityManager.getTransaction().begin();
        List<Terminal> resultList = entityManager.createNamedQuery("Terminal.findByUserid").setParameter("userid", userId).getResultList();
        entityManager.getTransaction().commit();
        return resultList;
    }
    
}